package com.scalar.forecast.forecastEntry.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.Constants;
import com.scalar.forecast.forecastEntry.forms.ForecastEntryForm;
import com.scalar.forecast.forecastEntry.RowData;
import com.scalar.forecast.forecastEntry.SlotData;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.text.DecimalFormat;

/**
 * User: Sujan Kumar
 * Date: Feb 18, 2005
 * Time: 4:06:17 PM
 */
public class ForecastEntryBuildNewCommand extends ActionCommand
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
    {
        ActionForward actionForward = mapping.findForward ( "buildSearchPage" );
        request.setAttribute ( Constants.FORECAST_IS_BUILD, "true" );
        ForecastEntryForm forecastEntryForm = ( ForecastEntryForm )form;
        forecastEntryForm.setBuild( true );
        actionForward = new ForecastEntrySalesNewCommand().execute( mapping, form, request, response );
        LinkedList rowDataList = forecastEntryForm.getTableDataList();
        RowData buildRowData = getBuildRowData ( rowDataList );
        forecastEntryForm.setBuildRowData( buildRowData );
        return actionForward;
    }
    private RowData getBuildRowData ( LinkedList rowDataList )
    {
        RowData buildRowData = new RowData();
        SlotData buildSlotData = null,slotData = null;
        LinkedList buildSlotDataList = new LinkedList();
        LinkedList slotDataList = null;
        RowData rowData = null;
        buildRowData.setSlotDataList( buildSlotDataList );
        int slotDataListSize = 0;
        if ( rowDataList != null && rowDataList.size() > 0 )
        {
            rowData = ( RowData ) rowDataList.get ( 0 );
            slotDataListSize = rowData.getSlotDataList().size();
            for ( int i = 0; i < slotDataListSize; i++ )
            {
                buildSlotDataList.add ( new SlotData() );
            }
        }

        int rowDataListSize = rowDataList == null ? 0 : rowDataList.size();
        int count = 0;
        for ( int slot = 0; slot < slotDataListSize; slot++ )
        {
            buildSlotData = ( SlotData )buildSlotDataList.get ( slot );
            count = 0;
            for ( int row = 0; row < rowDataListSize; row++ )
            {
                rowData = ( RowData )rowDataList.get ( row );
                if ( rowData.getMaxLevelOrder() == rowData.getLevelOrder() )
                {
                    slotDataList = rowData.getSlotDataList();
                    slotData = ( SlotData ) slotDataList.get ( slot );
                    buildSlotData.setQty( buildSlotData.getQty() + slotData.getQty() );
                    if ( slotData.getPrice() > 0 )
                    {
                        count++;
                        buildSlotData.setPrice ( buildSlotData.getPrice() + slotData.getPrice() );
                    }
                }
            }
            if ( count > 0 )
            {
                double avgPrice = buildSlotData.getPrice()  / count;
                DecimalFormat decimalFormat = new DecimalFormat( "############.00" );
                buildSlotData.setPrice( Double.parseDouble ( decimalFormat.format( avgPrice ) ) );
            }
        }
        return buildRowData;
    }
}

